<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/09/13
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Tpmecms;
use think\Db;
use think\Config;



/**
 * 维修店铺🔩🔧
 */
class Repair extends Tpmecms
{

    protected $noNeedLogin = ['get_all_route','get_route_repair_list','get_repair_data','get_repair_list'];
    protected $noNeedRight = ['*'];
    protected $db = 'repair';

    public function _initialize()
    {
        parent::_initialize();
        // $user = $this->auth->getUserinfo();

        // if($this->auth->group_id!=2){
        //     $this->error('非维修店铺角色');
        // }

        //找不到不需要登陆的方法
        if(!in_array($this->request->action(),$this->noNeedLogin)){
            $data['uid'] = $this->auth->id;
            $repair = Db::name($this->db)->where($data)->find();
            // halt($this->request->action());
            if(!$repair && $this->request->action()!='reg_repair'){
                $this->error('请注册维修店铺');
            }
        }
        
    }

    /**
     * 完善维修店铺信息🔩🔧
     *
     * @param string $name 维修店铺名称
     * @param string $lng 经度
     * @param string $lat 纬度
     * @param string $addr 详细地址
     * @param string $tel 联系电话
     * @param string $contacts 联系人
     * @param string $logo_image 维修店铺logo图片
     * @param string $images banner图
     * @param string $description 简介
     * @return void
     */
    public function reg_repair($name=null ,$lng=null ,$lat=null ,$addr=null ,$tel=null ,$contacts=null ,$logo_image=null ,$images=null ,$description=null)
    {
      
        $params = $this->request->param();
    
        $repair = Db::name($this->db)->where('uid',$this->auth->id)->find();
        if($repair){
            Db::name('user')->where('id',$this->auth->id)->update(['rule_status'=>'3']);//rule_status第二角色:0=无,1=加油站,2=二手车卖家,3=车辆维修,4=物流专线
            $this->error('您已注册维修店铺');
        }

        // halt($params);
        $fields = ['name'
                    ,'lng'
                    ,'lat'
                    ,'addr'
                    // ,'uid'
                    ,'tel'
                    ,'contacts'
                    ,'logo_image'
                    ,'images'
                    // ,'switch'
                    // ,'createtime'
                    // ,'updatetime'
                    ,'description'
                    ,'slogan'
                    ,'content'
                    ,'h_ps'
                    ,'h_toll'
                    ,'h_toll_night'
                    ,'b_1c_1b'
                    ,'b_1c_2b'
                    ,'b_tyre'
                    ,'b_ps'
                    ,'b_toll_night'
                    ,'year'
                    ,'toll'
                    ,'toll_night'
                    ,'ps'];
        $data = $this->get_field_arr($params,$fields,true);
        $data['uid'] = $this->auth->id;

        
        //判断是否有其他的店铺角色
        if($this->auth->rule_status>0){
            $this->error('您已有其他的店铺');
        }



        $data['switch'] = 1;//状态:0=不显示,1=正常显示
        $data['createtime'] = time();

        $id = Db::name($this->db)->insertGetId($data);
        if($id){
      
            $this->UpdateGeoRepair($lng,$lat,$id);//更新维修店铺定位
          
            //修改附属角色 rule_status第二角色:0=无,1=加油站,2=二手车卖家,3=车辆维修,4=物流专线
            Db::name('user')->where('id',$this->auth->id)->update(['rule_status'=>'3']);
            $this->success('注册成功');
        }

    }

    

    /**
     * 编辑维修店铺信息🔩🔧
     *
     * @param string $name 维修店铺名称
     * @param string $lng 经度
     * @param string $lat 纬度
     * @param string $addr 详细地址
     * @param string $tel 联系电话
     * @param string $contacts 联系人
     * @param string $logo_image 维修店铺logo图片
     * @param string $images banner图
     * @param string $description 简介
     * 
     * @return void
     */
    public function edit_repair($name = null ,$lng = null ,$lat = null ,$addr = null ,$tel = null ,$contacts = null ,$logo_image = null ,$images = null ,$description=null)
    {
        $where['uid'] = $this->auth->id;
        $repair = Db::name($this->db)->where($where)->find();
        if(!$repair){
            $this->error('您没有维修店铺');
        }elseif($repair['switch']!=1){
            $this->error('您的维修店铺已被关闭，请联系平台');
        }

        $params = $this->request->param();
        $fields = ['name'
                    ,'lng'
                    ,'lat'
                    ,'addr'
                    // ,'uid'
                    ,'tel'
                    ,'contacts'
                    ,'logo_image'
                    ,'images'
                    // ,'switch'
                    // ,'createtime'
                    // ,'updatetime'
                    ,'description'
                    ,'slogan'
                    ,'content'
                    ,'h_ps'
                    ,'h_toll'
                    ,'h_toll_night'
                    ,'b_1c_1b'
                    ,'b_1c_2b'
                    ,'b_tyre'
                    ,'b_ps'
                    ,'b_toll_night'
                    ,'year'
                    ,'toll'
                    ,'toll_night'
                    ,'ps'];
        $data = $this->get_field_arr($params,$fields,true);

        // $data['switch'] = 1;//状态:0=不显示,1=正常显示
        $data['updatetime'] = time();

        $res = Db::name($this->db)->where($where)->update($data);
        $this->UpdateGeoRepair($lng,$lat,$repair['id']);//更新维修店铺定位

        if($res){
            $this->success('修改成功');
        }else{
            $this->error('未修改内容');
        }


    }



    /**
     * 获取我的维修店铺信息🔩🔧
     *
     * @return void
     */
    public function get_my_repair_data()
    {
     
        $repair = $this->GetRepairDataByUid($this->auth->id);
        // if(!$repair){
        //     $this->error('您没有维修店铺');
        // }

        $this->success('ok',$repair);
    }




    /**
     * 获取维修店铺列表
     *
     * @param int $lat 纬度(可空)
     * @param int $lng 经度(可空)
     * @param int $limit 数10
     * @param int $page 页1
     * @param int $type 1维修，2补胎，3打黄油
     * @return void
     */
    public function get_repair_list($lat=null,$lng=null,$type=null,$limit=10,$page=1)
    {
        $where['switch'] = ['=',1];
        if($type){
            switch ($type) {
                case '1':
                    // $where['toll|toll_night'] = ['=','not null'];
                    // $where2 = 'toll|toll_night not null';
                    $list = Db::name($this->db)
                            ->where($where)
                            ->where('toll|toll_night','not null')
                            ->page($page,$limit)
                            ->select();
                    break;
                case '2':
                    $list = Db::name($this->db)
                            ->where($where)
                            ->where('b_1c_1b|b_1c_2b|b_toll_night|b_tyre','not null')
                            ->page($page,$limit)
                            ->select();
                    // $where['b_1c_1b|b_1c_2b|b_toll_night|b_tyre'] = ['=','not null'];
                    break;
                case '3':
                    $list = Db::name($this->db)
                            ->where($where)
                            ->where('h_toll|h_toll_night','not null')
                            ->page($page,$limit)
                            ->select();
                    // $where['h_toll|h_toll_night'] = ['=','not null'];
                    break;
                default:
                    break;
            }
        }else{
            $list = Db::name($this->db)
                    ->where($where)
                    // ->where('toll|toll_night','not null')
                    ->page($page,$limit)
                    ->select();
        }

        // halt($where);

        foreach ($list as $key => &$val) {
            $val['km'] = 0;
            if($lat && $lng){
                $val['km'] = round($this->get_coordinate_distance($lng,$lat,$val['lng'],$val['lat']),2);
            }
            
        }
        $this->success('ok',$list);
        


    }

    /**
     * 获取维修店铺信息
     *
     * @param int $repair_id 维修店铺id
     * @return void
     */
    public function get_repair_data($repair_id)
    {
        $this->success('ok',$this->GetRepairDataById($repair_id));
    }


}